#!/usr/bin/php
<?php
/**
 * Copyright (c) Enalean, 2012. All Rights Reserved.
 *
 * This file is a part of Tuleap.
 *
 * Tuleap is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Tuleap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This files is an example of soap api call
 * 
 * Usage: $argv[0] -h http://tuleap.example.com -u admin -p siteadmin -g 104
 */

function example($host, $user, $pass, $group_id) {
    try {
        $soap_options = array(
            'cache_wsdl' => WSDL_CACHE_NONE,
            'trace'      => 1,
        );
        
        // Establish connexion to the server...
        $soap = new SoapClient("http://$host/soap/?wsdl", $soap_options);
        // ...and get the session hash of the user.
        $session_hash = $soap->login($user, $pass)->session_hash;
        
        
        //Get statistics
        echo "Retrieving statistics for project $group_id\n";
        $soap  = new SoapClient("http://$host/plugins/statistics/soap/?wsdl", $soap_options);
        $stats = $soap->getProjectDiskStats($session_hash, $group_id);
        print_r($stats);

    } catch (Exception $e) {
        echo "ERROR: ". $e->getMessage() .PHP_EOL;
        echo "----- Last response:\n". $soap->__getLastResponse() ."\n-----\n";
    }
}









// read parameters from commandline + parameters
// You don't really need to read this for the example
$options  = getopt('g:u:p:h:');
$host     = isset($options['h']) ? $options['h'] : '';
$user     = isset($options['u']) ? $options['u'] : '';
$pass     = isset($options['p']) ? $options['p'] : '';
$group_id = isset($options['g']) ? (int)$options['g'] : '';


while (!$host) {
    fwrite(STDOUT, "\nHostname: ");
    $host = rtrim(fgets(STDIN), "\n");
}

while (!$user) {
    fwrite(STDOUT, "\nUsername: ");
    if ($new_user = rtrim(fgets(STDIN), "\n")) {
        $user = $new_user;
    }
}
echo 'Logging in as '. $user . PHP_EOL;

while (!$pass) {
    fwrite(STDOUT, 'Enter your password: ');
    $pass = get_password();
    echo PHP_EOL;
}

while (!$group_id) {
    fwrite(STDOUT, "\nProject id: ");
    $group_id = (int)rtrim(fgets(STDIN), "\n");
}
example($host, $user, $pass, $group_id);

function get_password($stars = false)
{
    // Get current style
    $oldStyle = shell_exec('stty -g');

    if ($stars === false) {
        shell_exec('stty -echo');
        $password = rtrim(fgets(STDIN), "\n");
    } else {
        shell_exec('stty -icanon -echo min 1 time 0');

        $password = '';
        while (true) {
            $char = fgetc(STDIN);

            if ($char === "\n") {
                break;
            } else if (ord($char) === 127) {
                if (strlen($password) > 0) {
                    fwrite(STDOUT, "\x08 \x08");
                    $password = substr($password, 0, -1);
                }
            } else {
                fwrite(STDOUT, "*");
                $password .= $char;
            }
        }
    }

    // Reset old style
    shell_exec('stty ' . $oldStyle);

    // Return the password
    return $password;
}
?>
